package com.kykj.kykjyzw.service.impl;

import com.kykj.kykjyzw.dao.RouteDao;
import com.kykj.kykjyzw.dao.UserDao;
import com.kykj.kykjyzw.entity.RouteViewEntity;
import com.kykj.kykjyzw.entity.UserRouteEntity;
import com.kykj.kykjyzw.service.RouteService;
import com.kykj.kykjyzw.vo.RouteVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RouteServiceImpl implements RouteService {

    @Resource
    UserDao userDao;

    @Resource
    RouteDao routeDao;

    /**
     * 获取当前用户所有权限
     * @param uname 用户名
     * @return List
     */
    @Override
    public List<RouteViewEntity> getUserRole(String uname) {
        List<RouteViewEntity> routeViewEntities = userDao.getUserRole(uname);//获取当前用户的所有权限

        return routeViewEntities;
    }

    /**
     * 判断URL是否包含在当前用户的权限中
     * @param uname 用户名
     * @param URI URI
     * @return boolean
     */
    @Override
    public boolean isRoute(String uname, String URI) {
        List<RouteViewEntity> routeViewEntities = userDao.getUserRole(uname);//获取当前用户的所有权限
        boolean flag=false;
        for(RouteViewEntity r: routeViewEntities
        ){
            if (URI.contains(r.getController())){
                flag=true;
                break;
            }
        }
        return flag;
    }

    @Override
    public int addRoute(RouteVo roureVo) {
        return routeDao.addRoute(roureVo);
    }

    @Override
    public int updateRoute(RouteVo roureVo) {
        return routeDao.updateRoute(roureVo);
    }

    @Override
    public int delRoute(int menuid) {
        return routeDao.delRoute(menuid);
    }

    @Override
    public UserRouteEntity getRouteByMenuId(int menuid) {
        return routeDao.getRouteByMenuId(menuid);
    }

    @Override
    public List<UserRouteEntity> getRouteList(String name,String controller, int start, int limit) {
        if (!name.equals("")){
            name="%"+name+"%";
        }
        if (!controller.equals("")){
            controller="%"+controller+"%";
        }
        return routeDao.getRouteList(name,controller,start,limit);
    }

    @Override
    public int getRouteCount() {
        return routeDao.getRouteCount();
    }
}
